import threading
import os
from django.http import StreamingHttpResponse, JsonResponse, HttpResponse
from django.shortcuts import render
from .global_vars import globalvars
from .decfun import whatever

# Create your views here.

stop_message = "stop"
start_message = "start"


def receive_message_deal(message):
    if not message:
        return "NULL"
    flag = 0
    for i in message:
        if i not in "0.123456789":
            flag = 1
            break
    if flag == 0:
        return eval(message)
    if message.lower() == "true":
        return True
    elif message.lower() == "false":
        return False
    else:
        return message


def solveInequ(request):
    global start_message
    if request.method == "POST":
        name = receive_message_deal(request.POST.get("name"))
        bilateral = receive_message_deal(request.POST.get("bilateral"))
        maxCount = receive_message_deal(request.POST.get("maxCount"))
        numInequ = receive_message_deal(request.POST.get("numInequ"))

        globalvars.message = start_message
        # 创建新线程
        t = threading.Thread(
            target=whatever.func, args=(name, bilateral, numInequ, maxCount)
        )
        t.start()

        return HttpResponse(status=200)
        # return render(request, 'app1/random.html')
    else:
        print("常恒你写错方式了！")
        return HttpResponse("请求方法错误！", status=405)


def get_type_two_num():
    global stop_message
    while globalvars.message != stop_message:
        if len(globalvars.progress) != 0:
            a = globalvars.progress.pop(0)
            a_progress = a["progress"]
            type_a = str(type(a_progress)).split("'")[1]
            a_recovered = a["recovered"]
            # print(f"data: {a_progress} {a_recovered} {type_a}")
            yield f"data: {a_progress} {a_recovered} {type_a}\n\n"  # 生成随机数
            # data: {data['message']}\nid: {data['id']}\n\n
    # yield "data: 1008611\n\n"
    while len(globalvars.progress) != 0:
        a = globalvars.progress.pop(0)
        a_progress = a["progress"]
        type_a = str(type(a_progress)).split("'")[1]
        a_recovered = a["recovered"]
        yield f"data: {a_progress} {a_recovered} {type_a}\n\n"


def solveInequ_SSE(request):
    response = StreamingHttpResponse(
        get_type_two_num(), content_type="text/event-stream"
    )
    response["Cache-Control"] = "no-cache"
    response["X-Accel-Buffering"] = "no"
    return response


def getDist(request):
    if request.method == "POST":
        startIndex = receive_message_deal(request.POST.get("startIndex"))
        endIndex = receive_message_deal(request.POST.get("endIndex"))
        steps = receive_message_deal(request.POST.get("steps"))

        # 将参数传入函数
        whatever.func2(startIndex, endIndex, steps)

        return JsonResponse({"dist": globalvars.dist, "key": globalvars.key})
    else:
        print("常恒你写错方式了！")


def exterminate(request):
    global stop_message
    try:
        globalvars.message = stop_message
        return HttpResponse(status=200)
    except:
        return HttpResponse(status=405)
